package scau.zxck.view.controller;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import scau.zxck.entity.Connection;
import scau.zxck.utils.DatabaseUtil;
import scau.zxck.utils.ViewUtil;
import scau.zxck.view.Main;
import scau.zxck.view.stage.AlertStage;
import java.util.regex.Pattern;

/**
 * Created by chenyulin on 2016/11/29.
 */
public class NewConnController {

    @FXML private TextField tfConnName;
    @FXML private TextField tfHost;
    @FXML private TextField tfUserName;
    @FXML private TextField tfPassword;
    @FXML private TextField tfPort;

    @FXML
    public void btTestAction() {
        String connName = tfConnName.getText();
        String host = tfHost.getText();
        String userName = tfUserName.getText();
        String password = tfPassword.getText();
        String port = tfPort.getText();
        Connection conn = new Connection(connName, host, userName, password, port);
        if (DatabaseUtil.testConnection(conn))
            new AlertStage("Connect Successfully").show();
        else
            new AlertStage("Error", "Failed to connect").show();
    }

    @FXML
    public void btOkAction() {
        String connName = tfConnName.getText();
        String host = tfHost.getText();
        String userName = tfUserName.getText();
        String password = tfPassword.getText();
        String port = tfPort.getText();
        Connection newConn = new Connection(connName, host, userName, password, port);
        if (regInfo(newConn)) {
            Main.connectionList.add(0, newConn);
            Main.connTreeController.initialize();
            ViewUtil.writeInfoToConfigFile();
            Main.newConnStage.close();
        }
    }

    /**
     * 新建连接信息校验
     * @param conn
     * @return
     */
    public static boolean regInfo(Connection conn) {
        String connName = conn.getConnName();
        String userName = conn.getUserName();
        String host = conn.getHost();
        String port = conn.getPort();
        String hostReg = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|[a-zA-z][a-zA-Z0-9-.]{0,70}$";
        String portReg = "^+?[1-9][0-9]*$";
        boolean isConnNameCorrect = !connName.equals("");
        boolean isUserNameCorrect = !userName.equals("");
        boolean isHostCorrect = Pattern.compile(hostReg).matcher(host).matches();
        boolean isPortCorrect = Pattern.compile(portReg).matcher(port).matches() && Integer.valueOf(port) < 65536;
        if (!isConnNameCorrect)
            new AlertStage("Please enter ConnName").show();
        else
            if(!isUserNameCorrect)
                new AlertStage("Please enter UserName").show();
            else
                if(!isHostCorrect)
                    new AlertStage("Host is not correct").show();
                else
                if(!isPortCorrect)
                    new AlertStage("Port is not correct").show();
        return isConnNameCorrect && isUserNameCorrect && isHostCorrect && isPortCorrect;
    }
}
